﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BolsaWeb.Negocio;
using BolsaWeb.Dominio;
using BolsaWeb.Models;
using System.Web.Security;
namespace BolsaWeb.Controllers
{
    public class UsuarioController : Controller
    {
        
        // GET: /Login/
        SeguridadService SeguridadService { get; set; }
        public ActionResult LogOn()
        {
            if (Session[SeguridadService.SESSION_NAME] != null)
            {
                return RedirectToAction("Index", "Home");
            }
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(Usuario formLogin)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Usuario AccountLogin = SeguridadService.ValidarUsuario(formLogin.Correo, formLogin.Clave);
                    Session[SeguridadService.SESSION_NAME] = AccountLogin;
                    SeguridadService.LogOn(AccountLogin.Correo);
                    return RedirectToAction("Index", "Home");
                }
                return View();
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex.Message);
                return View();
            }
        }
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            Session.RemoveAll();
            Session.Abandon();
            return RedirectToAction("Index", "Home");
        }

    }
}
